package com.th.util;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;

public class VerificationUtil {
    public static String VCODE;

    public static BufferedImage getVCODE() {
        VCODE = null;
        Random r = new Random();
        String c = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        // 创建image对象
        BufferedImage image = new BufferedImage(800, 150, BufferedImage.TYPE_INT_RGB);
        //创建画笔对象
        Graphics g = image.getGraphics();
        //画一个矩形
        g.fillRect(0, 0, 800, 150);
        StringBuffer sb = new StringBuffer();
        for (int i = 1; i <= 4; i++) {
            g.setColor(getColor());
            g.setFont(new Font("Consolas", Font.PLAIN, 100));
            char str = c.charAt(r.nextInt(62));
            sb.append(str);
            g.drawString(str + "", 150 * i, 80);
        }
        //画干扰线
        for (int i = 0; i < 80; i++) {
            g.setColor(Color.DARK_GRAY);
            int x1 = r.nextInt(800);
            int y1 = r.nextInt(150);
            int x2 = r.nextInt(800);
            int y2 = r.nextInt(150);
            g.drawLine(x1, y1, x2, y2);
        }
        VCODE = sb.toString();
        return image;
    }

    private static Color getColor() {
        int _r = new Random().nextInt(256);
        int g = new Random().nextInt(256);
        int b = new Random().nextInt(256);
        return new Color(_r, g, b);
    }
}
